﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using Beaup.UnitTests;

namespace Beaup
{
    
    public class UIConsole
    {
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    }

    public class Debug
    {
        public static void PrintMessage(string msg)
        {
#if DEBUG
            Console.WriteLine("DEBUG >> " + msg);
#endif
        }

        public static void UnitTest(string test)
        {
#if DEBUG
            try
            {
                string fullTest = "Beaup.UnitTests." + test;
                Type t = System.Reflection.Emit.TypeBuilder.GetType(fullTest, true);
                IUnitTester tester = (IUnitTester)t.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                tester.RunTest();
                //tester.PrintResults();
                tester.PrintErrors();
            }
            catch (TypeLoadException e)
            {
                Debug.PrintMessage(e.Message);
            }
            catch (NullReferenceException e)
            {
                Debug.PrintMessage(e.Message);
            }
#endif
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
#if DEBUG
            UIConsole.AllocConsole();            
#endif
            Debug.PrintMessage("Allocated Console For Process");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Debug.UnitTest("MSNPCommandTester"); //PASSED
            //Debug.UnitTest("MSNPClientStateMapTester"); //PASSED
            Application.Run(new BeaupSignIn());
            Debug.PrintMessage("Freeing Console For Process");
#if DEBUG           
            UIConsole.FreeConsole();
#endif
        }
    }
}
